using System;
using UnityEngine;
using UnityEngine.UI;


/// <summary>
/// </summary>
/// <typeparam name="T"> 继承自ViewContorller</typeparam>
/// <typeparam name="V"> 继承自View</typeparam>
public class ViewController<T,V>where T : new() where V : View,new()
{
    static T instance = new T();

    public static T Instance =>instance;

    public V view;

    /// <summary>
    /// 
    /// </summary>
    /// <param name="resPath"> resourcePath </param>
    /// <param name="isAddUpdateListen"></param>
    /// <param name="close"></param>
    /// <param name="stopAI_open"></param>
    public void Init(string resPath, bool isAddUpdateListen = false, bool stopAI_open = false)
    {
        view = new V();
        view.Init(resPath,isAddUpdateListen,Close,stopAI_open);

        GameEvent.ResetSortOrder += ResetSortOrder;
    }

    private void ResetSortOrder()
    {
        if(view != null && view._enable)
        {
            var o = view.canvas.sortingOrder - 10000;
            o = Mathf.Clamp(o, -30000, 30000);
            view.canvas.sortingOrder = o;
        }
    }

    public virtual void AddListener()
    {

    }

    public bool IsOpen()
    {
        return view._enable;
    }
    public void SetActive(bool active)
    {
        if(view._enable != active) 
        {
            view.gameObject.SetActive(active);
            ViewManager.Instance.OnViewChange_Begin(view.resPath,active);
            if (active)
            {
                view.OnEnable();
                ViewManager.Instance.order += 1;

                
                view.canvas.sortingOrder = ViewManager.Instance.order; 
                
                if(ViewManager.Instance.order >= 30000)
                {
                    ViewManager.Instance.order -= 10000;
                    GameEvent.ResetSortOrder?.Invoke();
                }
                view.SetSize(); //设置界面 可能会提供设置分辨率的功能
                view._enable = true;
            }
            else
            {
                view._enable = false;
                view.OnDisable();
            }
            ViewManager.Instance.OnViewChange_End(view.resPath,active);
        }
    }
    public void Open()
    {
        if(view.gameObject == null)
        {
            var go  = ResourceManager.Instance.Instantiate<GameObject>(view.resPath);
            GameObject.DontDestroyOnLoad(go);
            view .gameObject = go;
            view.gameObject.name = view.gameObject.name.Split('.')[0];
            view.transform = go.transform;
            view.canvas = go.GetComponent<Canvas>();    
            view.cs = go.GetComponent<CanvasScaler>();
            view.Awake();
            SetActive(true);
            view._enable = true;
            view.Start();
        }
        else
        {
            SetActive(true);
        }
    }

    public void Close(bool isClose =false)
    {
        if(view._enable == false)
        {
            return;
        }
        SetActive(false);
        Cursor.lockState = CursorLockMode.Locked;
        Cursor.visible = false;
    }

    public virtual void RemoveListener()
    {

    }

}
